{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 准备环境"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Use one of the following command to start using pyalink:\n",
      "使用以下一条命令来开始使用 pyalink：\n",
      " - useLocalEnv(parallelism, flinkHome=None, config=None)\n",
      " - useRemoteEnv(host, port, parallelism, flinkHome=None, localIp=\"localhost\", config=None)\n",
      "Call resetEnv() to reset environment and switch to another.\n",
      "使用 resetEnv() 来重置运行环境，并切换到另一个。\n",
      "\n",
      "JVM listening on 127.0.0.1:57785\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "JavaObject id=o6"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from pyalink.alink import *\n",
    "resetEnv()\n",
    "useLocalEnv(1, config=None)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 数据准备"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "## prepare data\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "data = np.array([\n",
    "    [0, 0.0, 0.0, 0.0],\n",
    "    [1, 0.1, 0.1, 0.1],\n",
    "    [2, 0.2, 0.2, 0.2],\n",
    "    [3, 9, 9, 9],\n",
    "    [4, 9.1, 9.1, 9.1],\n",
    "    [5, 9.2, 9.2, 9.2]\n",
    "])\n",
    "df = pd.DataFrame({\"id\": data[:, 0], \"f0\": data[:, 1], \"f1\": data[:, 2], \"f2\": data[:, 3]})\n",
    "inOp = BatchOperator.fromDataframe(df, schemaStr='id double, f0 double, f1 double, f2 double')\n",
    "FEATURE_COLS = [\"f0\", \"f1\", \"f2\"]\n",
    "VECTOR_COL = \"vec\"\n",
    "PRED_COL = \"pred\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 数据预处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "vectorAssembler = (\n",
    "    VectorAssembler()\n",
    "    .setSelectedCols(FEATURE_COLS)\n",
    "    .setOutputCol(VECTOR_COL)\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 聚类训练"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "kMeans = (\n",
    "    KMeans()\n",
    "    .setVectorCol(VECTOR_COL)\n",
    "    .setK(2)\n",
    "    .setPredictionCol(PRED_COL)\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 数据预测"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>f0</th>\n",
       "      <th>f1</th>\n",
       "      <th>f2</th>\n",
       "      <th>vec</th>\n",
       "      <th>pred</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.0 0.0 0.0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.0</td>\n",
       "      <td>0.1</td>\n",
       "      <td>0.1</td>\n",
       "      <td>0.1</td>\n",
       "      <td>0.1 0.1 0.1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2.0</td>\n",
       "      <td>0.2</td>\n",
       "      <td>0.2</td>\n",
       "      <td>0.2</td>\n",
       "      <td>0.2 0.2 0.2</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3.0</td>\n",
       "      <td>9.0</td>\n",
       "      <td>9.0</td>\n",
       "      <td>9.0</td>\n",
       "      <td>9.0 9.0 9.0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4.0</td>\n",
       "      <td>9.1</td>\n",
       "      <td>9.1</td>\n",
       "      <td>9.1</td>\n",
       "      <td>9.1 9.1 9.1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>5.0</td>\n",
       "      <td>9.2</td>\n",
       "      <td>9.2</td>\n",
       "      <td>9.2</td>\n",
       "      <td>9.2 9.2 9.2</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    id   f0   f1   f2          vec  pred\n",
       "0  0.0  0.0  0.0  0.0  0.0 0.0 0.0     1\n",
       "1  1.0  0.1  0.1  0.1  0.1 0.1 0.1     1\n",
       "2  2.0  0.2  0.2  0.2  0.2 0.2 0.2     1\n",
       "3  3.0  9.0  9.0  9.0  9.0 9.0 9.0     0\n",
       "4  4.0  9.1  9.1  9.1  9.1 9.1 9.1     0\n",
       "5  5.0  9.2  9.2  9.2  9.2 9.2 9.2     0"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pipeline = Pipeline().add(vectorAssembler).add(kMeans)\n",
    "pipeline.fit(inOp).transform(inOp).firstN(9).collectToDataframe()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
